package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.FindSavarityDto;
import com.woniu.defecttracking.entity.Project;
import com.woniu.defecttracking.entity.Savarity;
import com.woniu.defecttracking.exception.DefectTrackingException;
import com.woniu.defecttracking.mapper.SavarityMapper;
import com.woniu.defecttracking.service.SavarityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.velocity.runtime.parser.node.ASTFalse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class SavarityServiceImpl extends ServiceImpl<SavarityMapper, Savarity> implements SavarityService {

    @Override
    public FindSavarityDto findSavarity() {
        QueryWrapper<Savarity> isdel = new QueryWrapper<>();
        isdel.eq("isdel", 0);

        List<Savarity> list = list(isdel);



        FindSavarityDto findSavarityDto = new FindSavarityDto();
        findSavarityDto.setSavarities(list);
        return findSavarityDto;
    }

    @AddNum("savarityNum")
    @Override
    public boolean addSavarity(FindSavarityDto addSavarityDto) {
        Savarity savarity = ComUtil.copyProperties(addSavarityDto, Savarity.class);
        return save(savarity);
    }

    @Override
    public boolean updateSavarity(FindSavarityDto updateSavarityDto) {
        Savarity savarity = ComUtil.copyProperties(updateSavarityDto, Savarity.class);

        return updateById(savarity);
    }

    @Override
    public boolean deleteSavarity(List<Integer> ids) throws DefectTrackingException {
        List<Savarity> savarities = listByIds(ids);
        for (Savarity savarity : savarities) {
            if (savarity.getIsdel()==1){
                throw new DefectTrackingException("删除失败",433);
            }
        }
        savarities.forEach(a->a.setIsdel(1));
        saveOrUpdateBatch(savarities);
        return true;
    }

    @Override
    public boolean updateStatusUp(Integer id) {
        QueryWrapper<Savarity> objectQueryWrapper = new QueryWrapper<>();
        Savarity savarityId = getOne(objectQueryWrapper.eq("Savarity_id",id));
        if (savarityId.getStatus()==0){
            return false;
        }
        savarityId.setStatus(0);
        if (updateById(savarityId)){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStatusDown(Integer id) {
        QueryWrapper<Savarity> objectQueryWrapper = new QueryWrapper<>();
        Savarity savarityId = getOne(objectQueryWrapper.eq("Savarity_id",id));
        if (savarityId.getStatus()==1){
            return false;
        }
        savarityId.setStatus(1);
        if (updateById(savarityId)){
            return true;
        }
        return false;
    }

    @Override
    public FindSavarityDto findOpenSavarity() {
        QueryWrapper<Savarity> isdel = new QueryWrapper<>();
        isdel.eq("isdel", 0);

        isdel.eq("status",0);
        List<Savarity> list = list(isdel);

        FindSavarityDto findSavarityDto = new FindSavarityDto();
        findSavarityDto.setSavarities(list);
        return findSavarityDto;
    }
}
